/**
 * Created by JetBrains WebStorm.
 * User: Tonis
 * Date: 04.03.12
 * Time: 22:21
 * To change this template use File | Settings | File Templates.
 */

var SimpleAI = Player.extend({
    following:null,
    path:[],
    update:function () {
        this._super(); // call the inherited Player update!
        var action = this.evaluateField();
        switch (action) {
            case AIStates.FOLLOW:
                if (this.following) {
                    if (this.evaluateNearest()) {
                        this.getFollowingPath();
                    }
                    this.go();
                } else {
                    this.following = this.findNearest();
                    this.getFollowingPath();
                    this.go();
                }
                break;
            default:
                break;
        }

    },
    evaluateNearest:function () {
        for (var i = 0; i < players.length; i++) {
            if (players[i].id == this.following.id) {
                if (players[i].x != this.following.x || players[i].y != this.following.y) { //if is changed
                    this.following.x = players[i].x;
                    this.following.y = players[i].y;
                    return true;
                }
                return false;
            }
        }
    },
    go:function () {
        var nextStep = this.path[0];
        if (nextStep) {
            if (!this.isMoving()) {
                if (isPlaceFree(nextStep.x, nextStep.y)) {
                    this.moveX(nextStep.x - (this.x / grid));
                    this.moveY(nextStep.y - (this.y / grid));
                    this.path.shift(); //remove done step
                }
            }

        }
    },
    getFollowingPath:function () {
        if (!this.graph) {
            this.graph = lazyInit();
        }

        if (!this.isMoving()) { //if full step has been made
            var start = this.graph.nodes[(this.x / grid)][(this.y / grid)];
            var end = this.graph.nodes[Math.floor((this.following.x / grid))][Math.floor((this.following.y / grid))];
//            console.log(start);
//            console.log(end);
            this.path = astar.search(this.graph.nodes, start, end);
        }

    },
    findNearest:function () {
        var minDistance = 19 * 16 * 32;
        var nearestPlayer = null;

        for (var i = 0; i < players.length; i++) {
            var player = players[i];
            if (player.id != this.id) {
                var currentDistance = computeManhattanDistance(this, player);
                if (currentDistance < minDistance) {
                    minDistance = currentDistance;
                    nearestPlayer = player;
                }
            }
        }

        return {id:nearestPlayer.id, distance:minDistance, time:0, x:nearestPlayer.x, y:nearestPlayer.y};
    },
    evaluateField:function () {
        return AIStates.FOLLOW;
    }
});

function computeManhattanDistance(src, target) {
    var d1 = Math.abs(src.x - target.x);
    var d2 = Math.abs(src.y - target.y);
    return d1 + d2;
}

function lazyInit() {
    return new Graph(booleanMap);
}